<?php
/**
 * Created by 陈俊伟.
 * User: Administrator
 * Date: 2019-10-28
 * Time: 下午 2:52
 */

namespace App\Lib\PHPExcel;

class ExcelHandle
{
    protected $title = '';
    protected $arr = [];
    public function __construct($title = '订单',$arr = [])
    {
        $this->title = $title;

        $arr = $this->arrange($arr);

        $this->arr = $arr;

    }

    /*
     * 导出通用方法
     * */
    public function export()
    {
        $dir=dirname(__FILE__);//查找当前脚本所在路径
        require $dir."/PHPExcel.php";//引入PHPExcel
        if(!is_array($this->arr)){
            return false;
        }

        //实例化PHPExcel类， 等同于在桌面上新建一个excel
        $objPHPExcel=new \PHPExcel();
        //获取当前活动sheet
        $objSheet=$objPHPExcel->getActiveSheet();
        //给当前活动sheet起个名称
        $objSheet->setTitle($this->title);
        $objSheet->fromArray($this->arr);

        $objWriter= \PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');//生成excel文件
        header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
        header('Content-Disposition: attachment;filename="'.$this->title.'.xls'.'"');//告诉浏览器将输出文件的名称
        header('Cache-Control: max-age=0');//禁止缓存
        $objWriter->save("php://output");
    }

    /*
     * 读文件
     * */
    public static function read(){
        header("Content-type: text/html; charset=utf-8");
        $dir=dirname(__FILE__);//查找当前脚本所在路径
        require $dir."/PHPExcel.php";//引入PHPExcel
        $filePath = $_FILES ['file'] ['tmp_name']; //[文件名][临时路径-写死的]
        $PHPReader = new \PHPExcel_Reader_Excel2007();
        if(!$PHPReader->canRead($filePath)){
            $PHPReader = new \PHPExcel_Reader_Excel5();
            if(!$PHPReader->canRead($filePath)){
                echo 'no Excel';
                return;
            }
        }
        $PHPExcel = $PHPReader->load($filePath);
        $currentSheet = $PHPExcel->getSheet(0);         //读取第一张工作表
        $allColumn = $currentSheet->getHighestColumn(); //取得最大的列号
        $allRow = $currentSheet->getHighestRow();       //取得一共有多少行
        for($currentRow = 1;$currentRow <= $allRow;$currentRow++){
            for($currentColumn='A';$currentColumn<= $allColumn; $currentColumn++){
                $arr[$currentColumn] = (string)$currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();
            }
            $val[]=$arr;
        }

        if(count($val) > 1){
            unset($val[0]);
        }else{
            $val = [];
        }

        return $val;

    }

    /*
   * 读文件
   * */
    public static function read1($maxColumn = ''){
        header("Content-type: text/html; charset=utf-8");
        $dir=dirname(__FILE__);//查找当前脚本所在路径
        require $dir."/PHPExcel.php";//引入PHPExcel
        $filePath = $_FILES ['file'] ['tmp_name']; //[文件名][临时路径-写死的]
        $PHPReader = new \PHPExcel_Reader_Excel2007();
        if(!$PHPReader->canRead($filePath)){
            $PHPReader = new \PHPExcel_Reader_Excel5();
            if(!$PHPReader->canRead($filePath)){
                echo 'no Excel';
                return;
            }
        }
        $PHPExcel = $PHPReader->load($filePath);
        $currentSheet = $PHPExcel->getSheet(0);//读取第一个工作表
        $allRow = $currentSheet->getHighestRow();//取得一共有多少行
        //这两段很重要
        $allColumn = $currentSheet->getHighestColumn();//取得最大的列号
        $allColumn = \PHPExcel_Cell::columnIndexFromString($allColumn);//将列数转换为数字 列数大于Z的必须转  A->1  AA->27

        $arr = [];

        //从第一行开始读 第一行为标题
        for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
            //从第A列开始输出
            for ($currentColumn = 0; $currentColumn < $allColumn; $currentColumn++) {

                $strColumn  = \PHPExcel_Cell::stringFromColumnIndex($currentColumn);//将数字转换为字母  0->A  26->AA
                $val = $currentSheet->getCell($strColumn.$currentRow)->getValue();

                //将每列内容读取到数组中
                $arr[$currentRow][$strColumn] = trim($val);

            }
        }

        //删除全部为空的行
        foreach ($arr as $key => $vals) {
            $tmp = '';
            foreach ($vals as $v) {
                $tmp .= $v;
            }
            if (!$tmp) unset($arr[$key]);
        }

        if(count($arr) > 1){
            unset($arr[1]);
        }else{
            $arr = [];
        }

        return $arr;

    }

    public static function factory($name,...$args)
    {
        $class = 'App\Lib\PHPExcel\ManuOrder'.'\\'.$name;

        $reflect = new \ReflectionClass($class);

        return $reflect->newInstanceArgs($args);
    }
}